package com.quwan.common.result;

import com.quwan.common.exception.ExpCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@ApiModel(
        description = "返回信息"
)
@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(
            value = "错误码",
            required = true
    )
    private int code;

    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty(
            value = "返回消息",
            required = true
    )
    private String msg = "";

    private ApiResult(IResultCode ResultCode) {
        this.code = ResultCode.getCode();
    }

    private ApiResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ApiResult(IResultCode IResultCode, T data) {
        this.code = IResultCode.getCode();
        this.data = data;
    }

    public static ApiResult<String> failed() {
        return new ApiResult<>(ResultCode.FAIL);
    }

    public static <T> ApiResult<T> failed(String data) {
        return new ApiResult(ResultCode.INTERNAL_SERVER_ERROR.getCode(),data);
    }

    public static ApiResult<String> success() {
        return new ApiResult<>(ResultCode.SUCCESS);
    }

    public static ApiResult<String> successMsg() {
        return new ApiResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ResultCode.SUCCESS, data);
    }

    public static <T> ApiResult<T> successMsg(String data) {
        return new ApiResult(ResultCode.SUCCESS.getCode(),data);
    }

    public static ApiResult<String> error(String msg) {
        return new ApiResult<>(ResultCode.INTERNAL_SERVER_ERROR, msg);
    }

    public static ApiResult<String> error(int code, String msg) {
        return new ApiResult<>(code, msg);
    }

    public static ApiResult<String> error(IResultCode resultCode) {
        return new ApiResult<>(resultCode);
    }

    public static ApiResult<String> error(ExpCode expCode) {
        return new ApiResult<>(expCode.getCode(), expCode.getMsg());
    }
}
